package jxl.readXls;

import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.List;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

public class SqlMapClientUtil {

	/** 数据库配置信息 **/
	private static final String RESOURCE = "jxl/readXls/SqlMapConfig.xml";

	/**
	 * 插入任务列表
	 * 
	 * @param taskList
	 */
	public static void insertTaskList(List<Task> taskList) {
		SqlMapClient sqlMapClient = null;
		try {
			sqlMapClient = getSqlMapClient();
			sqlMapClient.startTransaction();
			for (Task task : taskList) {
				sqlMapClient.insert("insertTaskInfo", task);
			}
			sqlMapClient.commitTransaction();
			System.out.println("批量插入成功");
		} catch (Exception e) {
			System.out.println("批量插入失败");
			e.printStackTrace();
		} finally {
			try {
				sqlMapClient.endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	private static SqlMapClient getSqlMapClient() throws IOException {
		Reader reader = null;
		reader = Resources.getResourceAsReader(RESOURCE);
		return SqlMapClientBuilder.buildSqlMapClient(reader);
	}
}
